<?php

declare(strict_types=1);

namespace app\admin\controller\v1;

use app\admin\library\Auth;
use app\admin\model\AdminLog as ModelAdminLog;
use app\common\Backend;
use think\Request;
use Throwable;

class AdminLog extends Backend
{

    /**
     * 页面优化  下拉加载  loading 
     * 直播渲染
     * 篮球的文字直播
     * 赛事详情的tab组件切换有问题
     * 赛事进行中的渲染问题
     * api接口优化
     * 
     * 
     * 用户订单的展示
     * 后台赛事关联直播问题
     * 优惠卷列表
     * 优惠卷领取
     * 关注赛事功能+列表
     * 关注专家+列表
     * 客服
     * 直播间的打赏 聊天等
     * 明星直播列表
     * 专家列表
     * 专家详情
     */
    /**
     * @var
     */
    protected  $model;

    protected $preExcludeFields = ['create_time', 'admin_id', 'username'];

    protected $quickSearchField = ['title'];

    public function initialize(): void
    {
        parent::initialize();

        $this->model = new ModelAdminLog();
    }

    /**
     * 查看
     * @throws Throwable
     */
    public function index(): void
    {
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        if (!$this->auth->isSuperAdmin()) {
            $where[] = ['admin_id', '=', $this->auth->id];
        }
        $res = $this->model
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);

        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => '',
        ]);
    }



    /**
     * 删除
     * @param array $ids
     * @throws Throwable
     */
    public function del(array $ids = []): void
    {
        if (!$this->request->isDelete() || !$ids) {
            $this->error(('Parameter error'));
        }

        $where             = [];
        $dataLimitAdminIds = $this->getDataLimitAdminIds();
        if ($dataLimitAdminIds) {
            $where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
        }

        $pk      = $this->model->getPk();
        $where[] = [$pk, 'in', $ids];

        $count = 0;
        $data  = $this->model->where($where)->select();
        $this->model->startTrans();
        try {
            foreach ($data as $v) {
                $count += $v->delete();
            }
            $this->model->commit();
        } catch (Throwable $e) {
            $this->model->rollback();
            $this->error($e->getMessage());
        }
        if ($count) {
            $this->success(('Deleted successfully'));
        } else {
            $this->error(('No rows were deleted'));
        }
    }
}
